<?php

namespace app\controllers;

use yii\db\Query;
use yii\web\UploadedFile;
use yii\web\Controller;
use app\models\Admin;


class LoginController extends Controller{

    public $layout = false;
    public $enableCsrfValidation = false;
    public function actionIndex(){
        $name=(new Query())->from('yii_wxapp')->one();
        return $this->render('index',['name'=>$name]);
    }


     //登录
    public function actionLogin(){
     $data=\Yii::$app->request->post();
     $identity = Admin::findOne(['account' =>$data['account']]);

        if (\Yii::$app->getSecurity()->validatePassword($data['password'], $identity->password)) {
         //验证成功
          \Yii::$app->user->login($identity);
            return json_encode(['code'=>1,'msg'=>'登录成功']);
        } else {
          //验证失败
            return json_encode(['code'=>0,'msg'=>'密码错误']);
        }
    }

    //退出
    public function actionLogout(){
         \Yii::$app->user->logout();
        \Yii::$app->user->loginRequired();
    }

}
